/*
 * Copyright (C) 2011, EADS France
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.interpreter.backends.rdf;
import xowl.interpreter.frontends.VocRDF;

/**
 *
 * @author Laurent WOUTERS
 */
public class SimpleRDFRepository extends RDFRepository {
    private static final String typingOntology = "http://www.xowl.org/interpreter/typing";
    
    private RDFQueryEngine queryEngine;
    private xowl.lang.owl2.Ontology typingOnto;
    
    public SimpleRDFRepository(xowl.utils.logging.Logger log) {
        super(log);
        this.queryEngine = new BufferedQueryEngine(this);
        this.rdfGraph.addEventHandler(queryEngine, 2);
        this.evaluatorParts.add(queryEngine);
        this.defaultEvaluator.addPart(queryEngine);
        this.typingOnto = this.resolveOntology(typingOntology);
        loadResource("patches/owl2.owl", xowl.interpreter.OWL2Syntax.Functional);
    }
    
    @Override protected void onNewInterpretationAsClass(xowl.lang.runtime.Entity entity, xowl.lang.runtime.Class interpretation) {
        RDFIRIReference subject = rdfGraph.getEntityNode(entity.getHasIRI());
        RDFIRIReference prop = rdfGraph.getEntityNode(getIRI(VocRDF.rdfType));
        RDFIRIReference type = rdfGraph.getEntityNode(getIRI(VocRDF.owlClass));
        rdfGraph.onChange(defaultEvaluator, new RDFChange(new RDFTriple(subject, prop, type), typingOnto, true));
    }
    @Override protected void onNewInterpretationAsObjectProperty(xowl.lang.runtime.Entity entity, xowl.lang.runtime.ObjectProperty interpretation) {
        RDFIRIReference subject = rdfGraph.getEntityNode(entity.getHasIRI());
        RDFIRIReference prop = rdfGraph.getEntityNode(getIRI(VocRDF.rdfType));
        RDFIRIReference type = rdfGraph.getEntityNode(getIRI(VocRDF.owlObjectProperty));
        rdfGraph.onChange(defaultEvaluator, new RDFChange(new RDFTriple(subject, prop, type), typingOnto, true));
    }
    @Override protected void onNewInterpretationAsDataProperty(xowl.lang.runtime.Entity entity, xowl.lang.runtime.DataProperty interpretation) {
        RDFIRIReference subject = rdfGraph.getEntityNode(entity.getHasIRI());
        RDFIRIReference prop = rdfGraph.getEntityNode(getIRI(VocRDF.rdfType));
        RDFIRIReference type = rdfGraph.getEntityNode(getIRI(VocRDF.owlDataProperty));
        rdfGraph.onChange(defaultEvaluator, new RDFChange(new RDFTriple(subject, prop, type), typingOnto, true));
    }
    @Override protected void onNewInterpretationAsDatatype(xowl.lang.runtime.Entity entity, xowl.lang.runtime.Datatype interpretation) {
        RDFIRIReference subject = rdfGraph.getEntityNode(entity.getHasIRI());
        RDFIRIReference prop = rdfGraph.getEntityNode(getIRI(VocRDF.rdfType));
        RDFIRIReference type = rdfGraph.getEntityNode(getIRI(VocRDF.rdfsDatatype));
        rdfGraph.onChange(defaultEvaluator, new RDFChange(new RDFTriple(subject, prop, type), typingOnto, true));
    }
    @Override protected void onNewInterpretationAsAnnotationProperty(xowl.lang.runtime.Entity entity, xowl.lang.runtime.AnnotationProperty interpretation) {
        RDFIRIReference subject = rdfGraph.getEntityNode(entity.getHasIRI());
        RDFIRIReference prop = rdfGraph.getEntityNode(getIRI(VocRDF.rdfType));
        RDFIRIReference type = rdfGraph.getEntityNode(getIRI(VocRDF.owlAnnotationProperty));
        rdfGraph.onChange(defaultEvaluator, new RDFChange(new RDFTriple(subject, prop, type), typingOnto, true));
    }
}
